/// <license>
/// Copyright (c) 2002-2008, DotNetNuke Corporation
/// All rights reserved
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
/// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
/// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
/// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
/// of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
/// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
/// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
/// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
/// DEALINGS IN THE SOFTWARE.
/// </license>

#region Using Statements

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web;
using System.Xml.XPath;

using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Packages;
using DotNetNuke.Entities.Modules.Definitions;

using DotNetNuke.ModuleToolkit.Common;
using DotNetNuke.ModuleToolkit.Modules.Extensions;

#endregion

namespace DotNetNuke.ModuleToolkit.Modules.Installers
{
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// ModuleSkinInstaller is a component installer for installing Module Skins
    /// </summary>
    /// <history>
    ///     [cnurse]  10/11/2007  Created
    /// </history>
    /// -----------------------------------------------------------------------------
    public class ModuleExtensionInstaller : ComponentInstallerBase
    {

        #region Private Members

        private ExtensionInfo tempExtension;
        private string IExtensionClass;
        private string ModuleName;
        private string ExtensionName;

        #endregion

        #region Private Methods

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Deletes a Extension.
        /// </summary>
        /// <history>
        /// 	[cnurse]	10/13/2007  created
        /// </history>
        /// -----------------------------------------------------------------------------
        private void DeleteExtension()
        {
            try
            {
                //Attempt to get the Extension
                ExtensionInfo Extension = ExtensionController.GetExtensionByPackageID(Package.PackageID);

                if (Extension != null)
                {
                    ExtensionController.DeleteExtension(Extension);
                }
                Log.AddInfo(Extension.Name + " " + Constants.INSTALLER_UnRegistered);
            }
            catch (Exception ex)
            {
                Log.AddFailure(ex);
            }
        }

        #endregion

        #region Public Methods

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// The Commit method finalises the Install and commits any pending changes.
        /// </summary>
        /// <remarks>In the case of ModuleExtensions this is not neccessary</remarks>
        /// <history>
        /// 	[cnurse]	10/11/2007  created
        /// </history>
        /// -----------------------------------------------------------------------------
        public override void Commit()
        {
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// The Install method installs the module Extension component
        /// </summary>
        /// <history>
        /// 	[cnurse]	10/11/2007  created
        /// </history>
        /// -----------------------------------------------------------------------------
        public override void Install()
        {
            try
            {
                //Get the Module
                DesktopModuleController controller = new DesktopModuleController();
                DesktopModuleInfo module = controller.GetDesktopModuleByModuleName(ModuleName);

                if (module != null)
                {
                    tempExtension = ExtensionController.GetExtensionByName(module.DesktopModuleID, ExtensionName);
                    
                    ExtensionInfo Extension = new ExtensionInfo();
                    Extension.PackageId = Package.PackageID;
                    Extension.DesktopModuleId = module.DesktopModuleID;
                    Extension.Name = ExtensionName;
                    Extension.IExtensionClass = IExtensionClass;

                    ExtensionController.AddExtension(Extension);

                    Completed = true;
                    Log.AddInfo(Extension.Name + " " + Constants.INSTALLER_Registered);
                }
            }
            catch (Exception ex)
            {
                Completed = false;
                Log.AddFailure(ex);
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// The ReadManifest method reads the manifest file for the Extension compoent.
        /// </summary>
        /// <history>
        /// 	[cnurse]	10/11/2007  created
        /// </history>
        /// -----------------------------------------------------------------------------
        public override void ReadManifest(XPathNavigator manifestNav)
        {
            //Get the name
            ExtensionName = Util.ReadElement(manifestNav, "Extension/ExtensionName", Log, Constants.INSTALLER_NameMissing);

            //Get the module name
            ModuleName = Util.ReadElement(manifestNav, "Extension/moduleName", Log, Constants.INSTALLER_ModuleNameMissing);

            //Get the IExtension Class
            IExtensionClass = Util.ReadElement(manifestNav, "Extension/IExtensionClass", Log, Null.NullString);

            if (Log.Valid)
                Log.AddInfo(Constants.INSTALLER_ReadSuccess);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// TThe Rollback method undoes the installation of the component in the event 
        /// that one of the other components fails
        /// </summary>
        /// <history>
        /// 	[cnurse]	10/11/2007  created
        /// </history>
        /// -----------------------------------------------------------------------------
        public override void Rollback()
        {
            //If Temp Extension exists then we need to update the DataStore with this 
            if (tempExtension == null)
            {
                //No Temp Extension - Delete newly added Extension
                DeleteExtension();
            }
            else
            {
                ExtensionController.AddExtension(tempExtension);
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// The UnInstall method uninstalls the Extension component
        /// </summary>
        /// <history>
        /// 	[cnurse]	10/11/2007  created
        /// </history>
        /// -----------------------------------------------------------------------------
        public override void UnInstall()
        {
            DeleteExtension();
        }

        #endregion

    }
}
